import CNumber from './number'

export enum IOPERATORS {
    PLUS = 'PLUS', // 加
    SUBT = 'SUBT', // 减
    MULT = 'MULT', // 乘
    DIVI = 'DIVI'  // 除
}

type IOPERATOR = {
    label: string
    value: IOPERATORS
    signLabel: string
    sign: string
}

export const OPERATORS: IOPERATOR[] = [
    { label: '加', value: IOPERATORS.PLUS, signLabel: '+', sign: '+' },
    { label: '减', value: IOPERATORS.SUBT, signLabel: '-', sign: '-' },
    { label: '乘', value: IOPERATORS.MULT, signLabel: '×', sign: '*' },
    { label: '除', value: IOPERATORS.DIVI, signLabel: '÷', sign: '/' }
]

export default class COperate {
    constructor (range?: number) {
        // ...
    }

    private random (operators: IOPERATOR[]) {
        const n = new CNumber()
        const idx = n.smallThen(operators.length - 1)
        return operators[idx]
    }

    randomIn (optValues: IOPERATORS[]) {
        if (optValues.length <= 0) {
            return this.random(OPERATORS)
        }

        if (optValues.length === 1) {
            return OPERATORS.find(item => item.value === optValues[0])!
        }
        
        const operators = OPERATORS.filter(item => optValues.includes(item.value))
        return this.random(operators)
    }
}